<%--
  Created by IntelliJ IDEA.
  User: chen
  Date: 17-9-17
  Time: 下午12:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>

<link rel="stylesheet" href="${basePath}/static/css/bootstrap.min.css">
<link rel="stylesheet" href="${basePath}/static/css/all.css" >

<script src="${basePath}/static/js/jquery-3.2.1.min.js"></script>
<script src="${basePath}/static/js/bootstrap.min.js"></script>
<script src="${basePath}/static/js/function.js" type="text/javascript"></script>

<div class="iframe_div">
    <table id="table_userPassword" class="table table-striped table-bordered table-hover  table-condensed iframe-table">
        <tbody>
        <tr>
            <td><label for="modifyPassword_username">用户名</label></td>
            <td><input id="modifyPassword_username" disabled="true" class="form-control" value="${sessionScope.get("loginUser").getUsername()}" /></td>
        </tr>
        <tr>
            <td><label for="modifyPassword_old">旧密码</label></td>
            <td><input type="password" id="modifyPassword_old" class="form-control"  /></td>
        </tr>
        <tr>
            <td><label for="modifyPassword_new_1">新密码</label></td>
            <td><input type="password" id="modifyPassword_new_1"  class="form-control" /></td>
        </tr>
        <tr>
            <td><label for="modifyPassword_new_2" id="user_data_student_name">请重新输入新密码</label></td>
            <td><input type="password" id="modifyPassword_new_2" class="form-control"/></td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                 <a id="modify_password" class="btn btn-default" status="1" style="width: 200px">修改密码</a>
            </td>
        </tr>
        </tbody>
    </table>
</div>
<script type="text/javascript">
    /**
     * 完成密码修改的ajax操作
     */
    var input_oldPassword = $("input#modifyPassword_old");
    var input_newPassword1 = $("input#modifyPassword_new_1");
    var input_newPassword2 = $("input#modifyPassword_new_2");

    $("a#modify_password").click(function () {
        var oldPassword = input_oldPassword.val();
        if(oldPassword === ""){       //排除未输入当前密码
            alert("请先输入当前密码");
            return;
        }
        if (oldPassword != ${sessionScope.get("loginUser").getPassword()}){     //排除输入密码错误
            alert("密码错误");
            input_oldPassword.val("");
            return;
        }

        var newPassword1 = input_newPassword1.val();
        var newPassword2 = input_newPassword2.val();

        if(newPassword1 === "" || newPassword2 === ""){
            alert("请输入完整");
            return;
        }

        if(newPassword1 !== newPassword2){                      //输入的新密码不一致
            input_newPassword1.val("");
            input_newPassword2.val("");
            alert("两次输入的密码不一致,请重新输入")
            return;
        }

        /**
         * 都正确之后完成密码的更新操作
         */
        var URL = "${basePath}/user/updateUserInfo";       //构造URL
        $.ajax({
            url:URL,
            type:"POST",
            data:{
                username:${sessionScope.get("loginUser").getUsername()},
                password:newPassword1
            },
            dataType:"text",
            success:function (data) {
                alert(data);
                window.location.href = "userData";
            }
        });
    });
</script>
